package com.sinosoft.emplmis.application.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class UserStatusInterceptor extends HandlerInterceptorAdapter{

	private Logger logger = LoggerFactory.getLogger(UserStatusInterceptor.class.getName());
	
	/**
	 * 用户状态拦截器处理，当用户状态为睡眠时，拒绝请求
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		logger.debug("用户状态拦截器启动，拦截请求路径为：" + request.getRequestURI());
		Session session = SecurityUtils.getSubject().getSession();
		if(session.getAttribute("userStatus") != null && session.getAttribute("userStatus").toString().equals("sleep")) {
			response.setHeader("userStatus", "sleep");
			response.getWriter().print("<p>系统被锁定，请刷新页面解锁!<p>");
			logger.debug("用户状态拦截器停止，用户状态为睡眠");
			return false;
		}
		logger.debug("用户状态拦截器停止，用户状态为活动");
		return true;
	}
}
